﻿
games.game9 = {

    //goblin game
    worldmap: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
    renderloop: undefined,
    npcRenderLoop: undefined,
    npcThinkLoop: undefined,
    stopGame: function () {
        window.clearInterval(this.renderloop);
        window.clearInterval(this.npcRenderLoop);
        window.clearInterval(this.npcThinkLoop);
    },
    init: function () {
        var html = "";
        html += "<table id='game9-goblin'>";
        for (var i = 0; i < this.worldmap.length; i++) {
            var worldmapRow = this.worldmap[i];
            html += "<tr class='row{0}'>".replace("{0}", i);
            for (var k = 0; k < worldmapRow.length; k++) {
                var worldmapCol = worldmapRow[k];
                html += "<td class='col{1} {0}'>&nbsp;</td>".replace("{1}", k).replace("{0}", (worldmapCol == 0 ? "wall" : worldmapCol == 1 ? "floor" : "obstacle"));
            }
            html += "</tr>";
        }
        html += "</table>";
        $("#gamezone").html(html);
        $(document).keyup(function (event) {
            games.game9.player.handleKey(event.keyCode, true);
            games.game9.player.handleKey(event.keyCode, false);
            return false;
        });
        this.renderloop = setInterval(function () {
            games.game9.player.draw();
            $("#game9-goblin .npc").removeClass("npc");
            for (var i = 0; i < games.game9.npcs.length; i++) {
                if (i < games.game9.npcs.length) {
                    var coords = games.game9.npcs[i];
                    games.game9.npc.draw(coords);
                }
            }
        }, 10);
        this.npcThinkLoop = setInterval(function () {
            for (var i = 0; i < games.game9.npcs.length; i++) {
                if (i < games.game9.npcs.length) {
                    var coords = games.game9.npcs[i];
                    if (coords[0] > 0 && coords[1] > 0) {
                        coords = games.game9.npc.think(coords)
                    }
                }
            }
        }, 250);
        this.npcRenderLoop = setInterval(function () {
            games.game9.npcs[games.game9.npcs.length] = [games.helper.getRandomNumber(10, 20), games.helper.getRandomNumber(10, 20)];
            games.game9.npc.timeOfBirth -= 100;
            if (games.game9.npc.timeOfBirth < 1000) {
                games.game9.npc.timeOfBirth = 1000
            }
        }, games.game9.npc.timeOfBirth);
    },
    npcs: new Array(),
    npc: {
        timeOfBirth: 5000,
        think: function (coords) {
            //decide new co-ords
            var choice = games.helper.getRandomNumber(0, 100);
            var x = coords[0];
            var y = coords[1];
            if (choice < 25) {
                x--;
            } else if (choice < 50) {
                y--;
            } else if (choice < 75) {
                x++;
            } else if (choice < 100) {
                y++;
            }
            var wallHit = $("#game9-goblin .row{0} .col{1}".replace("{0}", y).replace("{1}", x)).hasClass("wall");
            if (!wallHit) {
                coords[0] = x;
                coords[1] = y;
            }
            return coords;
        },
        draw: function (coords) {
            $("#game9-goblin .row{0} .col{1}".replace("{0}", coords[1]).replace("{1}", coords[0])).addClass("npc");
        }
    },
    player: {
        score: 0,
        coords: [1, 1],
        lastDirection: "d",
        isAttacking: false,
        handleKey: function (charCode, isUp) {
            //81 drop bomb blow up bomb
            if (isUp) {
                switch (charCode) {
                    case 88:
                        games.game9.player.attack();
                        break;
                }
            } else { // if (!games.game9.player.isAttacking) {
                var x = games.game9.player.coords[0];
                var y = games.game9.player.coords[1];
                switch (charCode) {
                    case 37:
                        x--; games.game9.player.lastDirection = "l";
                        break;
                    case 38:
                        y--; games.game9.player.lastDirection = "u";
                        break;
                    case 39:
                        x++; games.game9.player.lastDirection = "r";
                        break;
                    case 40:
                        y++; games.game9.player.lastDirection = "d";
                        break;
                }
                var wallHit = $("#game9-goblin .row{0} .col{1}".replace("{0}", y).replace("{1}", x)).hasClass("wall");
                if (!wallHit) {
                    games.game9.player.coords[0] = x;
                    games.game9.player.coords[1] = y;
                }
            }
            if (charCode == 37 || charCode == 38 || charCode == 39 || charCode == 40 || charCode == 88) {
                return false;
            }
        },
        attack: function () {
            games.game9.player.isAttacking = true;
            setTimeout(function () { games.game9.player.isAttacking = false; }, 1000);
        },
        draw: function () {
            $("#game9-goblin td").removeClass("player")
            $("#game9-goblin td").removeClass("sword")
            $("#game9-goblin .row{0} .col{1}".replace("{0}", this.coords[1]).replace("{1}", this.coords[0])).addClass("player");
            if (this.isAttacking) {
                var x = this.coords[0];
                var y = this.coords[1];
                switch (this.lastDirection) {
                    case "l":
                        x--;
                        break;
                    case "u":
                        y--;
                        break;
                    case "r":
                        x++;
                        break;
                    case "d":
                        y++;
                        break;
                }
                $("#game9-goblin .row{0} .col{1}".replace("{0}", y).replace("{1}", x)).addClass("sword");
                if ($("#game9-goblin .sword").hasClass("npc")) {
                    for (var i = 0; i < games.game9.npcs.length; i++) {
                        var coords = games.game9.npcs[i];
                        if ((coords[0] == x && coords[1] == y)) {
                            $("#game9-goblin .sword").removeClass("npc");
                            games.game9.npcs[i] = [-1, -1];
                            games.game9.player.score++;
                        }
                    }
                }
            }
            for (var i = 0; i < games.game9.npcs.length; i++) {
                var npcCoords = games.game9.npcs[i];
                if (npcCoords[0] == this.coords[0] && npcCoords[1] == this.coords[1]) {
                    $("#game9-goblin td").removeClass("player")
                    alert("uh oh, you didn't make it. Score was " + this.score);
                    games.game9.stopGame();
                }
            }
            //games.game9.player.bombPosition
        }
    }
};